package com.yunhe.tianhe.rentcars.controller.permission;

import com.yunhe.tianhe.commons.dto.Pageable;
import com.yunhe.tianhe.rentcars.dto.JSONResponse;
import com.yunhe.tianhe.rentcars.entity.permission.Account;
import com.yunhe.tianhe.rentcars.entity.permission.Role;
import com.yunhe.tianhe.rentcars.entity.system.Dept;
import com.yunhe.tianhe.rentcars.service.permission.AccountService;
import com.yunhe.tianhe.rentcars.service.permission.RoleService;
import com.yunhe.tianhe.rentcars.service.system.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lanyangyun
 * @version V1.0
 * @Description: //TODO
 * @date 2017/10/25 0025 下午 2:45
 */
@Controller
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountService us;

    @Autowired
    private RoleService rs;

    @Autowired
    private DeptService ds;

    @RequestMapping
    public String list(HttpServletRequest req){
        List<Dept> depts = ds.findList();
        req.setAttribute("depts",depts);
        return "/permission/account/list";
    }

    @RequestMapping("/add")
    public String add(){
        return "/permission/account/add";
    }

    @RequestMapping("/edit")
    public String edit(HttpServletRequest req, Long id){
        Account account = us.findOne(id);
        List<Role> roles = rs.findAll();
        List<Dept> depts = ds.findList();
        req.setAttribute("user",account);
        req.setAttribute("roles",roles);
        req.setAttribute("depts",depts);
        return "/permission/account/edit";
    }

    @RequestMapping("/save")
    public String save( Account account){
        us.save(account);
        String phone = account.getMobile();
        String email = account.getEmail();
        String password = account.getUserPwd();
        String accountName = account.getUserName();
        String code = "SMS_106335019";
        if (null!=phone){
            Map map = new HashMap<>();
            map.put("userName",accountName);
            map.put("password",password);
            try {
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        if (null!=email){

        }
        return "redirect:/account";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JSONResponse delete(String ids){
        us.deleteBatch(ids);
        return JSONResponse.success("删除成功！");
    }

    @RequestMapping("/update")
    public String update( Account account){
        us.update(account);
        return "redirect:/account";
    }

    @RequestMapping("/pageData")
    @ResponseBody
    public Pageable<Account> findPage(Integer offset, Integer limit){
        Pageable<Account> pageable = us.findPage(offset, limit);
        return pageable;
    }

    @RequestMapping("/dept")
    @ResponseBody
    public JSONResponse findDept(){
        List<Dept> depts = ds.findList();
        return JSONResponse.success("",depts);
    }

    @RequestMapping("/role")
    @ResponseBody
    public JSONResponse findPage(){
        List<Role> roles = rs.findAll();
        return JSONResponse.success("",roles);
    }

    @RequestMapping("/verify/name")
    @ResponseBody
    public JSONResponse verifyName(String name){
        Boolean verifyName = us.verifyName(name);
        return JSONResponse.success("",verifyName);
    }

    @RequestMapping("/search")
    @ResponseBody
    public Pageable<Account> search(Account account){
        Pageable<Account> pageable = us.search(account);
        return pageable;
    }
}
